﻿using MiniExcelLibs;
using MiniExcelLibs.Attributes;
using MiniExcelLibs.Csv;
using MiniExcelLibs.OpenXml;
using System.Diagnostics;
using System.IO;
using System.Windows;

namespace OperaExcel
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string filePath = "";
        string fileTemplatPath = "";
        public MainWindow()
        {
            InitializeComponent();
            filePath = AppDomain.CurrentDomain.BaseDirectory + @"Results";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            fileTemplatPath = AppDomain.CurrentDomain.BaseDirectory + @"Templates";
            if (!Directory.Exists(fileTemplatPath))
            {
                Directory.CreateDirectory(fileTemplatPath);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var path = Path.Combine(filePath, $"动态列添加.xlsx");
                if (File.Exists(path))
                    File.Delete(path);
                var config = new OpenXmlConfiguration
                {
                    AutoFilter = false,
                    DynamicColumns = new[]
                    {
                        new DynamicExcelColumn("列1") { Format = "@", Width = 25 },
                        new DynamicExcelColumn("列2") { Format = "@" , Width = 50}
                    }
                };
                var value = new[] { new { 列1 = "3333", 列2 = "22222222222222222" } };
                MiniExcel.SaveAs(path, value, configuration: config);
                //打开文件
                PathHelper.OpenFileByExcel(path);
            }
            catch
            {
                MessageBox.Show("操作失败");
            }
        }
        //按照模版导出Excel
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                var path = Path.Combine(filePath, $"按照模版导出Excel{DateTime.Now.Second}.xlsx");
                var templatePath = Path.Combine(fileTemplatPath, $"Template_Datetime.xlsx");
                if (File.Exists(path))
                    File.Delete(path);
                //添加集合数据到Excel
                List<ExcelDateTimeData> list = new();
                for (int i = 0; i < 5; i++)
                {
                    list.Add(new ExcelDateTimeData { ID = i + 1, Name = $"李四{i}", Age = 20 + i, Fh = "↑", Od = 1.26843 + i, Time = new DateTime(2025, 01, 01, 12, 12 + i, 12) });
                }
                var value = new { ExcelDateTimeData = list };
                MiniExcel.SaveAsByTemplate(path, templatePath, value);
                PathHelper.OpenFileByExcel(path);

                ////读取Excel数据到集合
                //var config = new OpenXmlConfiguration()
                //{
                //    EnableAutoWidth = true,
                //    DynamicColumnFirst = false
                //};
                //var queryData = MiniExcel.Query<ExcelDateTimeData>(path, configuration: config, startCell: "A1").ToList();
                //foreach (var row in queryData)
                //{
                //    Debug.WriteLine($"{row.ID}——{row.Name}——{row.Age}——{row.Od}——{row.Fh}——{row.Time}");
                //}
            }
            catch
            {
                MessageBox.Show("操作失败");
            }
        }
    }
}